package com.yopai.service.impl;

import com.yopai.mapper.GptInfoMapper;
import com.yopai.model.GptInfoDO;
import com.yopai.service.HttpGptService;
import com.yopai.service.HttpGptService;
import com.yopai.utils.CustomChatGpt;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Description
 * @Author 湧哥
 * @Version 1.0
 */
@Service
public class GetMasImpl implements HttpGptService {
    @Autowired
    private GptInfoMapper gptInfoMapper;
    @Override
    public String getMsg(String info) throws IOException {
        GptInfoDO gptInfoDO = new GptInfoDO();
        gptInfoDO.setInfo(info);
        gptInfoMapper.insert(gptInfoDO);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String apiKey = "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";
        CustomChatGpt customChatGpt = new CustomChatGpt(apiKey);
        customChatGpt.setResponseTimeout(80000);
        long start = System.currentTimeMillis();
        String answer = customChatGpt.getAnswer(httpClient, info);
        long end = System.currentTimeMillis();
        System.out.println("该回答花费时间为：" + (end - start) / 1000.0 + "秒");
        System.out.println("该回答花费时间为：" + (end - start) / 1000.0 + "秒");
        httpClient.close();
        return answer;
    }
}
